import layx from "vue-layx";

export default {
  namespaced: true,
  state:{
    iframe: [],
    collapsed: false,
  },
  getters: {

  },
  mutations: {
    // 新开窗口
    open(state, window){
      let target = state.iframe.filter(item=>new RegExp(window.path.split('/').slice(0,-1).join('/')).test(item.path))
      if(target.length>0){
        if(target[0].path!=window.path){
          this.commit('window/setUrl',{id:window.id,url:window.path})
          if(target[0].status==0){
            this.commit('window/restore',target[0].id)
          }
        }else{
          if(target[0].status==0){
            this.commit('window/restore',target[0].id)
          }else{
            this.commit('window/flicker',target[0].id)
          }
        }
      }else{
        state.iframe.push({...window,status: 1})
        layx.iframe('win'+window.id,window.title,window.path,{
          icon: `<img src="${window.icon||'http://www.winfoc.com/windows_test/static/images/common/ico.png'}" style="height:18px;width:18px;display:block;" />` ||'',
          storeStatus:this.state.setting.storeStatus,
          width: 1000,
          minWidth: 640,
          height: 600,
          minHeight: 600,
          border: false,
          status: 'default',
          position:[Math.round(Math.random()*(100-40)+80),Math.round(Math.random()*(100-40)+200)],
          event:{
            onmin:{
              after:()=>this.commit('window/setStatus',{id:window.id,value:0})
            },
            onmax:{
              after:()=>this.commit('window/setStatus',{id:window.id,value:2})
            },
            onrestore:{
              after:(layxWindow,winform)=>{
                let status = 1;
                if(winform.minBefore=='max') status = 2
                this.commit('window/setStatus',{id:window.id,value:status})
              }
            },
            ondestroy:{
              after:()=>this.commit('window/setStatus',{id:window.id,value:-1})
            },
          },
          style:layx.multiLine(function(){/*
            .layx-window{
              color: unset;
              border-radius: 6px !important;
              box-shadow: 0px 0px 4px !important;
            }
            .layx-inlay-menus .layx-icon:hover{
              color: #141414;
            }
            .layx-window.layx-min-statu {
              display:none !important;
            }
            .layx-window .layx-control-bar .layx-title,.layx-window .layx-control-bar .layx-left-bar{
              cursor:move;
            }
            @keyframes layx-flicker {
              from {
                transform: scale(1);
              }
              to {
                transform: scale(0.996);
              }
            }
            @-webkit-keyframes layx-flicker {
              from {
                transform: scale(1);
              }
              to {
                transform: scale(0.996);
              }
            }
          */})
        });
      }
    },
    // 最小化窗口
    min(state, id){
      layx.min('win'+id);
    },
    // 最大化窗口
    max(state, id){
      layx.max('win'+id);
    },
    // 恢复窗口
    restore(state,id){
      layx.restore('win'+id);
    },
    // 关闭窗口
    destroy(state,id){
      layx.destroy('win'+id);
    },
    // 关闭全部窗口
    destroyAll(){
      layx.destroyAll();
    },
    // 重载窗口
    reload(state,id){
      layx.reloadFrame('win'+id);
    },
    // 窗口闪烁
    flicker(state,id){
      layx.flicker('win'+id);
    },
    // 重置url
    setUrl(state,value){
      state.iframe = state.iframe.map(item=>{
        if(item.id==value.id){
          item.path = value.url
        }
        return item;
      })
      window.parent.document.getElementById('layx-win'+value.id+'-iframe').src=value.url
    },
    // 设置状态
    setStatus(state,obj){
      let{ id, value } = obj;
      state.iframe.filter((item,index)=>{
        if(item.id==id){
          if(value==-1){
            state.iframe.splice(index,1)
          }else{
            state.iframe[index].status = value
          }
        }
      })
    },
    
  },
}